package com.ruoyi.security.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfDeviceInfo;
import com.ruoyi.security.service.IAfDeviceInfoService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 设备信息Controller
 *
 * @author cjie
 * @date 2021-07-24
 */
@Api(tags = " 设备信息")
@RestController
@RequestMapping("/afDeviceInfo")
public class AfDeviceInfoController extends BaseController {
    @Autowired
    private IAfDeviceInfoService afDeviceInfoService;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询设备信息列表
     */
    @ApiOperation(value="查询设备信息列表", notes="查询设备信息列表")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(AfDeviceInfo afDeviceInfo) {
        startPage();
        List<AfDeviceInfo> list = afDeviceInfoService.selectAfDeviceInfoList(afDeviceInfo);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @ApiOperation(value="导出设备信息列表", notes="导出设备信息列表")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfDeviceInfo afDeviceInfo) throws IOException {
        List<AfDeviceInfo> list = afDeviceInfoService.selectAfDeviceInfoList(afDeviceInfo);
        ExcelUtil<AfDeviceInfo> util = new ExcelUtil<AfDeviceInfo>(AfDeviceInfo.class);
        util.exportExcel(response, list, "设备信息数据");
    }
    /**
     * 获取设备信息详细信息
     */
    @ApiOperation(value="获取设备信息详细信息", notes="获取设备信息详细信息")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afDeviceInfoService.selectAfDeviceInfoById(id));
    }

    /**
     * 新增设备信息
     */
    @ApiOperation(value="新增设备信息", notes="新增设备信息")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfDeviceInfo afDeviceInfo) {
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(afDeviceInfo.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        afDeviceInfo.setDeviceNum(nowCode);
        afDeviceInfo.setDeviceCategory(deviceType);
        return toAjax(afDeviceInfoService.insertAfDeviceInfo(afDeviceInfo));
    }

    /**
     * 修改设备信息
     */
    @ApiOperation(value="修改设备信息", notes="修改设备信息")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfDeviceInfo afDeviceInfo) {
        return toAjax(afDeviceInfoService.updateAfDeviceInfo(afDeviceInfo));
    }

    /**
     * 删除设备信息
     */
    @ApiOperation(value="删除设备信息", notes="删除设备信息")
    @ApiImplicitParam(name = "ids", value = "设备信息ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afDeviceInfo:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afDeviceInfoService.deleteAfDeviceInfoByIds(ids));
    }
}
